import { Provide, Inject } from '@midwayjs/core';
import { InjectEntityModel } from '@midwayjs/typeorm';
import { TB_DPChangeSample_Info } from '../entity/sampleInfo.entity';
import { Repository, In } from 'typeorm';
import { DPModelInfoQueryDto, DPModelInfoAddDto, DPModelInfoUpdateDto } from '../dto/SampleInfo.dto';

@Provide()
export class SampleService {
  @InjectEntityModel(TB_DPChangeSample_Info)
  sampleRepository: Repository<TB_DPChangeSample_Info>;

  // 添加样本信息
  async addSample(params: DPModelInfoAddDto) {
    try {
      let sample = new TB_DPChangeSample_Info();
      sample.QTime = params.QTime;
      sample.QFileName = params.QFileName;
      sample.QFilePath = params.QFilePath;
      sample.HTime = params.HTime;
      sample.HFileName = params.HFileName;
      sample.HFilePath = params.HFilePath;
      sample.CHShpName = params.CHShpName;
      sample.ShpField = params.ShpField;
      sample.CHShpPath = params.CHShpPath;
      sample.SampleCount = params.SampleCount;
      sample.Res = params.Res;
      sample.Size = params.Size;
      sample.SampleType = params.SampleType;
      sample.Status = params.Status;

      const result = await this.sampleRepository.save(sample);
      return {
        success: true,
        data: result,
        message: '新增样本信息成功',
      };
    } catch (error) {
      return {
        success: false,
        message: '新增样本信息失败: ' + error.message,
      };
    }
  }

  // 获取所有样本信息
  async getAllSamples() {
    try {
      const samples = await this.sampleRepository.find({
        order: {
          CreateTime: 'DESC',
        },
      });
      return {
        success: true,
        data: samples,
        message: '获取所有样本信息成功',
      };
    } catch (error) {
      return {
        success: false,
        message: '获取所有样本信息失败: ' + error.message,
      };
    }
  }

  // 根据查询条件获取样本信息，支持分页
  async findSamples(query: DPModelInfoQueryDto) {
    const { sampleType, sampleSizes, resolutions, page = 1, limit = 10 } = query;

    const where: any = {};

    // 根据样本类型查询
    if (sampleType) {
      where['SampleType'] = sampleType;
    }

    // 根据样本大小查询
    if (sampleSizes && sampleSizes.length > 0) {
      where['Size'] = In(sampleSizes);
    }

    // 根据分辨率查询
    if (resolutions && resolutions.length > 0) {
      where['Res'] = In(resolutions);
    }

    try {
      // 计算分页的起始位置
      const [samples, total] = await this.sampleRepository.findAndCount({
        where,
        skip: (page - 1) * limit,  // 跳过的条数
        take: limit,               // 每页返回的条数
      });

      return {
        success: true,
        data: samples,
        total,                      // 返回总条数
        message: '查询样本信息成功',
      };
    } catch (error) {
      return {
        success: false,
        message: '查询样本信息失败: ' + error.message,
      };
    }
  }

  // 删除样本信息
  async deleteSample(id: number) {
    try {
      const sample = await this.sampleRepository.findOne({
        where: { OBJID: id },
      });
      if (!sample) {
        return {
          success: false,
          message: '要删除的样本不存在',
        };
      }

      await this.sampleRepository.remove(sample);
      return {
        success: true,
        message: '删除样本信息成功',
      };
    } catch (error) {
      return {
        success: false,
        message: '删除样本信息失败: ' + error.message,
      };
    }
  }

  // 批量更新样本类型
  async batchUpdateSampleType(sampleType: string, sampleIds: number[]) {
    try {
      const samples = await this.sampleRepository.find({
        where: {
          OBJID: In(sampleIds),
        },
      });

      // 更新多个样本的类型
      for (let sample of samples) {
        sample.SampleType = sampleType;
        await this.sampleRepository.save(sample);
      }

      return {
        success: true,
        message: '批量更新样本类型成功',
      };
    } catch (error) {
      return {
        success: false,
        message: '批量更新样本类型失败: ' + error.message,
      };
    }
  }
}
